﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Intex.Db.Service
{
    public class ItemValueFacory
    {
        private static Dictionary<string, IValueConvert> valueConverts = new Dictionary<string, IValueConvert>();
        private static Dictionary<string, Func<IValueConvert>> creators = new Dictionary<string, Func<IValueConvert>>();
        public static void Register(string name, Func<IValueConvert> creator)
        {
            if (creators.ContainsKey(name))
            {
                creators[name] = creator;
                if (valueConverts.ContainsKey(name))
                {
                    valueConverts[name] = creator();
                }
            }
            else
            {
                creators.Add(name, creator);
            }
        }

        public static string ConvertToValue(string name, object value, Models.ItemType itemType, Models.TableItem row)
        {
            if (!valueConverts.ContainsKey(name))
            {
                if (creators.ContainsKey(name))
                {
                    valueConverts.Add(name, creators[name]());
                }
                else
                {
                    return string.Empty;
                }
            }
            return valueConverts[name].ExecuteToGetData(value, itemType, row);
        }

        public static string ConvertToValue(string name, object value, Models.ItemType itemType, System.Data.DataRow row)
        {
            if (!valueConverts.ContainsKey(name))
            {
                if (creators.ContainsKey(name))
                {
                    valueConverts.Add(name, creators[name]());
                }
                else
                {
                    return string.Empty;
                }
            }
            return valueConverts[name].ExecuteToGetData(value, itemType, row);
        }
    }
}
